<template>
<view class="container">
  <view class="attend_list">
    <view class="attend_item" v-for="(item, index) in attendList" :key="index">
      <view class="attend_item_info">
        <view class="attend_item_left">
          <image :src="item.attendPlatformUserIcon + '?x-oss-process=style/preview_120'" class="attend_head_icon"></image>
          <text class="attend_name">{{item.attendPlatformUserName}}</text>
        </view>
        <view class="attend_item_right">
          <text class="attend_time">{{item.attendTime}}</text>
        </view>
      </view>
      <!-- 操作显示多规格产品 -->
      <view class="more_measure_btn" v-if="item.attendMeasureList.length!=0" :data-id="item.id" @click.stop="showMeasureListFun">
        <image class="more_measure_btn_icon" v-if="!showMeasureListState['showMeasureListState_'+item.id]" src="https://image1.sansancloud.com/xianhua/2019_8/2/15/22/51_366.jpg?x-oss-process=style/preview_120"></image>
        <image class="more_measure_btn_icon" v-else src="https://image1.sansancloud.com/xianhua/2019_8/2/15/21/34_281.jpg?x-oss-process=style/preview_120"></image>
        <text v-if="!showMeasureListState['showMeasureListState_'+item.id]">展开</text>
        <text v-else>收起</text>
      </view>
      <!-- 多规格产品 -->
      <view class="measure_list" v-if="showMeasureListState['showMeasureListState_'+item.id]">
        <view class="measure_item" v-for="(measureItem, index2) in item.attendMeasureList" :key="index2">
          <view class="measure_item_name">{{measureItem.attendMeasureName}}</view>
          <view class="measure_item_price">￥{{measureItem.attendPrice}}</view>
          <view class="measure_item_stock">{{measureItem.buyCount}}件</view>
        </view>
      </view>
    </view>
  </view>
</view>
</template>

<script>

export default {
  data() {
    return {
      attendList: {},
      showMeasureListState: {},
      animationData: {},
      pageData: {
        totalSize: 0,
        curPage: 1,
        pageSize: 20
      },

      /**
       * 生命周期函数--监听页面加载
       */
      params: {},
      loginUser: "",
      setting: ""
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    console.log('===options===', options);
    let that = this;
    that.params = options;
    that.setData({
      setting: getApp().globalData.setting,
      loginUser: getApp().globalData.loginUser
    });
    let title = "";

    if (options.type == "my") {
      console.log("=========我的参与记录=========");
      that.getMyAttendListData(options);
      title = "我的参与记录";
    } else if (options.type == "more") {
      that.getAttendListData(options);
      title = "更多参与记录";
      console.log("=========更多参与记录=========");
    } else {
      that.getAttendListData(options);
      title = "更多参与记录";
      console.log("=========其他=========");
    }

    wx.setNavigationBarTitle({
      title: title
    });
    console.log("===loginUser====", that.loginUser);
    console.log("===setting====", that.setting);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getSessionUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.pageData.curPage = 1;

    if (that.params.type == "my") {
      that.getMyAttendListData(that.params);
    } else if (that.params.type == "more") {
      that.getAttendListData(that.params);
    } else {
      that.getAttendListData(that.params);
    }

    that.setData({
      setting: getApp().globalData.setting,
      loginUser: getApp().globalData.loginUser
    });
    console.log("===loginUser====", that.loginUser);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   * pageData:{
        totalSize:0,
        curPage:1,
        pageSize:20
      },
   */
  onReachBottom: function () {
    console.log('===onReachBottom====');
    let that = this;

    if (that.pageData.totalSize > that.pageData.curPage * that.pageData.pageSize) {
      that.pageData.curPage += 1;

      if (that.params.type == "my") {
        that.getMyAttendListData(that.params);
      } else if (that.params.type == "more") {
        that.getAttendListData(that.params);
      } else {
        that.getAttendListData(that.params);
      }
    } else {
      wx.showToast({
        title: "到底了~",
        image: '/wxcomponents/images/icons/tip.png',
        duration: 1000
      });
    }
  },
  methods: {
    tolinkUrl: function (e) {
      let linkUrl = e.currentTarget.dataset.link;
      getApp().globalData.linkEvent(linkUrl);
    },
    showMeasureListFun: function (e) {
      let that = this;
      console.log("=======showMeasureListFun========", e);
      let productId = e.currentTarget.dataset.id;
      let showMeasureListState = that.showMeasureListState;
      let state = showMeasureListState['showMeasureListState_' + productId];

      for (let i in showMeasureListState) {
        showMeasureListState[i] = false;
      }

      showMeasureListState['showMeasureListState_' + productId] = !state;
      that.setData({
        showMeasureListState: showMeasureListState
      });
      let showMeasureListState2 = showMeasureListState['showMeasureListState_' + productId];
      let animation = wx.createAnimation({
        duration: 400,
        timingFunction: 'ease'
      });
      console.log("=======popupFormPage==========", animation, this.showMeasureListState2);

      if (showMeasureListState2) {
        animation.bottom('100rpx').step();
      } else {
        animation.bottom('-100rpx').step();
      }

      this.setData({
        animationData: animation.export()
      });
    },

    /* 获取数据 */
    getAttendListData: function (params) {
      let that = this;
      let param = Object.assign({}, params, {
        page: that.pageData.curPage
      });
      let customIndex = getApp().globalData.AddClientUrl("/wx_find_custom_form_commit_attend_list.html", param);
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log('====getAttendListData-res===', res);
          let showMeasureListState = that.showMeasureListState;

          if (res.data.errcode == 0) {
            let dataArr = res.data.relateObj.result;

            if (that.pageData.curPage == 1) {
              that.attendList = [];
            } else {
              dataArr = that.attendList.concat(dataArr);
            }

            console.log("=========attendList===========", dataArr);

            for (let i = 0; i < dataArr.length; i++) {
              if (dataArr[i].attendMeasureList && typeof dataArr[i].attendMeasureList == "string") {
                dataArr[i].attendMeasureList = JSON.parse(dataArr[i].attendMeasureList);
              }

              showMeasureListState['showMeasureListState_' + dataArr[i].id] = false;
            }

            that.setData({
              attendList: dataArr,
              showMeasureListState: showMeasureListState
            });
            that.pageData.pageSize = res.data.relateObj.pageSize;
            that.pageData.totalSize = res.data.relateObj.totalSize;
          } else {
            wx.showModal({
              title: '提示',
              content: '主人~请求超时！确认重新加载',
              success: function (res) {
                if (res.confirm) {
                  that.getAttendListData(that.params);
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          }

          console.log('===attendList===', that.attendList);
          console.log('===myAttend===', that.myAttend);
        },
        complete: function (res) {}
      });
    },

    /* 获取数据 */
    getMyAttendListData: function (params) {
      let that = this;
      let param = Object.assign({}, params, {
        page: that.pageData.curPage
      });
      let customIndex = getApp().globalData.AddClientUrl("/wx_user_attend_custom_form_commit.html", param);
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log('====getAttendListData-res===', res);
          let showMeasureListState = that.showMeasureListState;

          if (res.data.errcode == 0) {
            let dataArr = res.data.relateObj.result;

            if (that.pageData.curPage == 1) {
              that.attendList = [];
            } else {
              dataArr = that.attendList.concat(dataArr);
            }

            console.log("=========attendList===========", dataArr);

            for (let i = 0; i < dataArr.length; i++) {
              if (dataArr[i].attendMeasureList && typeof dataArr[i].attendMeasureList == "string") {
                dataArr[i].attendMeasureList = JSON.parse(dataArr[i].attendMeasureList);
              }

              showMeasureListState['showMeasureListState_' + dataArr[i].id] = false;
            }

            that.setData({
              attendList: dataArr,
              showMeasureListState: showMeasureListState
            });
            that.pageData.pageSize = res.data.relateObj.pageSize;
            that.pageData.totalSize = res.data.relateObj.totalSize;
          } else {
            wx.showModal({
              title: '提示',
              content: '主人~请求超时！确认重新加载',
              success: function (res) {
                if (res.confirm) {
                  that.getAttendListData(that.params);
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          }

          console.log('===attendList===', that.attendList);
          console.log('===myAttend===', that.myAttend);
        },
        complete: function (res) {}
      });
    },
    getSessionUserInfo: function () {
      var that = this;
      getApp().globalData.showToastLoading('loading', true);
      var postParamUserBank = getApp().globalData.AddClientUrl("/get_session_userinfo.html", {}, 'post');
      wx.request({
        url: postParamUserBank.url,
        data: postParamUserBank.params,
        header: getApp().globalData.headerPost,
        success: function (res) {
          console.log(res.data);
          let data = res.data.relateObj;

          if (res.data.errcode == '0') {
            that.setData({
              loginUser: data
            });
          } else {
            wx.showToast({
              title: res.data.errMsg,
              image: '/wxcomponents/images/icons/tip.png',
              duration: 1000
            });
          }
        },
        fail: function (res) {
          wx.hideLoading();
          wx.showToast({
            title: "请求错误",
            image: '/wxcomponents/images/icons/tip.png',
            duration: 1000
          });
          console.log(res.data);
        },
        complete: function (res) {
          wx.hideLoading();
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>